package com.nan.leetcode.剑指offer第二版.包含min函数的栈;

import java.util.Stack;

public class MinStack {
    public Stack<Integer> stack1;
    public Stack<Integer> stack2;
    public MinStack() {
        this.stack1=new Stack<>();
        this.stack2=new Stack<>();
    }

    public void push(int x) {
        stack1.push(x);//-2,0,-3
        if(stack2.isEmpty()||x<=stack2.peek()){
            stack2.push(x);//-2,-3
        }
    }

    public void pop() {//栈A中元素出栈，如果栈B中栈顶元素和A中出栈的一致，B中的也要出栈
        int y=stack1.pop();
        if(stack2.peek().equals(y)){
            stack2.pop();
        }
    }

    public int top() {
        return stack1.peek();
    }

    public int min() {
        return stack2.peek();
    }

    public static void main(String[] args) {
        MinStack minStack=new MinStack();//调用构造方法
        minStack.push(-2);
        minStack.push(0);
        minStack.push(-3);
        System.out.println(minStack.min());
        minStack.pop();
        System.out.println(minStack.top());
        System.out.println(minStack.min());
    }
}
